using UnityEngine;
using QFramework;
using ProjectSurvivor;
using System;


// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class Enemy : ViewController, IEnemy
	{
		public float HP = 3;
		public float MovementSpeed = 2.0f;
		//溶解颜色
		public Color DissolveColor = Color.red;
		//是否是宝箱怪
		public bool TreasureChestEnemy=false;
		void Start()
		{
			// Code Here
			//敌人生成数量++
			EnemyGenerator.EnemyCount.Value++;
		}
		void OnDestroy()
		{
			//删掉数量--
			EnemyGenerator.EnemyCount.Value--;
		}
		void FixedUpdate()
		{
			if (!mIgnoreHurt)
			{
				//玩家存在
				if (Player.Default)
				{
					//计算方向
					var direction = (Player.Default.transform.position - transform.position).normalized;
					//向玩家移动
					SelfRigidbody2D.velocity = direction * MovementSpeed;
				}
				//玩家不存在
				else
				{
					//设置速度为0
					SelfRigidbody2D.velocity = Vector2.zero;
				}
			}

		}
		bool isOver=false;
		private void Update()
		{
			//血量为0
			if (HP <= 0&&!isOver)
			{
				isOver=true;
				//掉落
				Global.GeneratePowerUp(gameObject,TreasureChestEnemy);
				//溶解
				FxController.Play(Sprite, DissolveColor);
				//需要引入QAssetBundle的引用，否则会报错
				AudioKit.PlaySound("Enemy_die");
				//删除
				this.DestroyGameObjGracefully();
			}

		}
		//忽略伤害
		private bool mIgnoreHurt = false;
		/// <summary>
		/// 扣血
		/// </summary>
		/// <param name="value"></param>
		public void Hurt(float value, bool force = false, bool critical = false)
		{
			//如果忽略伤害并且不是强制伤害	
			if (mIgnoreHurt & !force) return;
			//设置忽略伤害为true
			mIgnoreHurt=true;
			//设置速度为0
			SelfRigidbody2D.velocity=Vector2.zero;
			//飘血
			FloatingTextController.Play(transform.position, value.ToString("0"),critical);
			//变红
			Sprite.color = Color.red;
			AudioKit.PlaySound("Hit");
			//推迟 
			ActionKit.Delay(0.2f, () =>
			{
				//扣血
				HP -= value;
				//变白
				Sprite.color = Color.white;
				//设置忽略伤害为false
				mIgnoreHurt = false;
			}).Start(this);//开始
		}

		//设置血量缩放
		public void SetHPScale(float hPScale)
		{
			//血量乘以缩放
			HP *= hPScale;
		}
		//设置速度缩放
		public void SetSpeedScale(float speedScale)
		{
			//速度乘以缩放
			MovementSpeed *= speedScale;
		}
	}
}
